package com.ruoyi.appraise.controller;

import com.ruoyi.appraise.domain.UserAppraise;
import com.ruoyi.appraise.service.IUserAppraiseService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.check.CheckEmptyUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.order.mapper.TechnicianItemOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.List;

/**
 * 用户评价Controller
 *
 * @author ruoyi
 * @date 2025-04-09
 */
@RestController
@RequestMapping("/userAppraise/userAppraise")
public class UserAppraiseController extends BaseController {
    @Autowired
    private IUserAppraiseService userAppraiseService;
    @Resource
    private TechnicianItemOrderMapper technicianItemOrderMapper;

    /**
     * 查询用户评价列表
     */
    @PreAuthorize("@ss.hasPermi('userAppraise:userAppraise:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserAppraise userAppraise) {
        if (!CheckEmptyUtil.isEmpty(userAppraise.getTName())){
            List<Long> tIds = technicianItemOrderMapper.seleTid(userAppraise.getTName());
            if (!CheckEmptyUtil.isEmpty(tIds)){
                userAppraise.setTids(tIds);
            }else {
                return getDataTable(Collections.emptyList());
            }
        }else {
            userAppraise.setTids(Collections.emptyList());
        }
        startPage();
        List<UserAppraise> list = userAppraiseService.selectUserAppraiseList(userAppraise);
        if (!CheckEmptyUtil.isEmpty(list)) {
            for (UserAppraise order : list) {
                String userName = technicianItemOrderMapper.selectUserName(order.getUserId());
                order.setUserName(CheckEmptyUtil.isEmpty(userName) ? "" : userName);
                String tName = technicianItemOrderMapper.selectTName(order.getTid());
                order.setTName(CheckEmptyUtil.isEmpty(tName) ? "" : tName);
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出用户评价列表
     */
    @PreAuthorize("@ss.hasPermi('userAppraise:userAppraise:export')")
    @Log(title = "用户评价", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserAppraise userAppraise) {
        if (!CheckEmptyUtil.isEmpty(userAppraise.getTName())){
            List<Long> tIds = technicianItemOrderMapper.seleTid(userAppraise.getTName());
            if (!CheckEmptyUtil.isEmpty(tIds)){
                userAppraise.setTids(tIds);
            }
        }
        List<UserAppraise> list = userAppraiseService.selectUserAppraiseList(userAppraise);
        if (!CheckEmptyUtil.isEmpty(list)) {
            for (UserAppraise order : list) {
                String userName = technicianItemOrderMapper.selectUserName(order.getUserId());
                order.setUserName(CheckEmptyUtil.isEmpty(userName) ? "" : userName);
                String tName = technicianItemOrderMapper.selectTName(order.getTid());
                order.setTName(CheckEmptyUtil.isEmpty(tName) ? "" : tName);
            }
        }
        ExcelUtil<UserAppraise> util = new ExcelUtil<UserAppraise>(UserAppraise.class);
        util.exportExcel(response, list, "用户评价数据");
    }

    /**
     * 获取用户评价详细信息
     */
    @PreAuthorize("@ss.hasPermi('userAppraise:userAppraise:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(userAppraiseService.selectUserAppraiseById(id));
    }

    /**
     * 新增用户评价
     */
    @PreAuthorize("@ss.hasPermi('userAppraise:userAppraise:add')")
    @Log(title = "用户评价", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserAppraise userAppraise) {
        return toAjax(userAppraiseService.insertUserAppraise(userAppraise));
    }

    /**
     * 修改用户评价
     */
    @PreAuthorize("@ss.hasPermi('userAppraise:userAppraise:edit')")
    @Log(title = "用户评价", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserAppraise userAppraise) {
        return toAjax(userAppraiseService.updateUserAppraise(userAppraise));
    }

    /**
     * 删除用户评价
     */
    @PreAuthorize("@ss.hasPermi('userAppraise:userAppraise:remove')")
    @Log(title = "用户评价", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(userAppraiseService.deleteUserAppraiseByIds(ids));
    }
}
